﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;

namespace MvcApplication2.Controllers
{
   

    public class HelloController : Controller
    {
        //person
        public class Person
        {
            public long Id { get; set; }
            public string Firstname { get; set; }
            public string Surname { get; set; }
            public int Age { get; set; }

            public Address Address { get; set; }
        }
        public class Address
        {
            public string Street { get; set; }
            public string Cap { get; set; }
            public string City { get; set; }
        }

        //chess
        public class GameState
        {
            public List<ChessMan> AllChessMen;
            public string WhoStrokeFirst;
            public string WhoPlayWhite;
            //public GameState();
        }
        public class ChessMan
        {

            public string Name;
            public int X;
            public int Y;

            //public ChessMan();
        }

        //car
        public class Carobj
        {
            public Car car { get; set; }
            public string email { get; set; }
            public string phone { get; set; }
        }
        public class Car
        {

            public string make { get; set; }
            public string model { get; set; }
        }


        public ActionResult Inde()
        {
            return View();

        }

        //GameState
        public ActionResult Res(string id)
        {
            string ms = "привет это строка";
            List<ChessMan> df = new List<ChessMan>();
            ChessMan ch1 = new ChessMan();
            ch1.Name = "Tyra";
            ch1.X = 1;
            ch1.Y = 2;
            df.Add(ch1);

            GameState gs = new GameState();
            gs.AllChessMen = df;
            gs.WhoPlayWhite = "gerbert";
            gs.WhoStrokeFirst = "ываываываыв";

            //df.Add("dfgdfg");
            //df.Add("dfg");
            return Json(gs, JsonRequestBehavior.AllowGet); ;

            //return Json(ms,"text/json",Encoding.UTF8, JsonRequestBehavior.AllowGet);
            //return Json(ms, JsonRequestBehavior.AllowGet); ;
        }

        //ChessMan
        [HttpPost]
        public ActionResult ResChessMan(ChessMan CM)
        {
            CM.X = 5;
            return Json(CM, JsonRequestBehavior.AllowGet); ;
        }

        //Carobj
        public ActionResult ResCar(string jsonData)
        {
            using (Stream str = new MemoryStream(Encoding.Unicode.GetBytes(jsonData)))
            {
                DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Carobj));
                var response = js.ReadObject(str);
            }
            var otvet = new { id = 1, value = "new" };
            return Json(otvet, JsonRequestBehavior.AllowGet); ;

            //return Json(ms,"text/json",Encoding.UTF8, JsonRequestBehavior.AllowGet);
            //return Json(ms, JsonRequestBehavior.AllowGet); ;
        }
       
        //Carobj
        [HttpPost]
        public ActionResult ResCarPost(string jsonData)
        {
            Stream str = new MemoryStream(Encoding.Unicode.GetBytes(jsonData));
            DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Carobj));
            var response = js.ReadObject(str);


            return Json("get ок", JsonRequestBehavior.AllowGet); ;

            //return Json(ms,"text/json",Encoding.UTF8, JsonRequestBehavior.AllowGet);
            //return Json(ms, JsonRequestBehavior.AllowGet); ;
        }

        //class Person
        [HttpPost]
        public ActionResult CreatePerson(Person person)
        {
            return Json(person, JsonRequestBehavior.AllowGet); //dummy example, just serialize back the received Person object
        }
        public ActionResult SomeActionMethod(int asd)
        {
            Car car = new Car();
            car.make = "fgfg";
            car.model = "bmv";
            return Json(car, JsonRequestBehavior.AllowGet);
        }
        public ActionResult HiThere(int asd)
        {
            
            Redirect("HiThere.aspx");
            return View();
        }
    }
}
